package com.tarena.lbs.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.dao.repository.CategoryRepository;
import com.tarena.lbs.content.pojo.param.ArticleCategoryParam;
import com.tarena.lbs.content.pojo.po.ArticleCategoryPO;
import com.tarena.lbs.content.pojo.query.ArticleCategoryQuery;
import com.tarena.lbs.content.pojo.vo.ArticleCategoryVO;
import com.tarena.lbs.content.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryRepository categoryRepository;

    @Override
    public PageResult<ArticleCategoryVO> pageList(ArticleCategoryQuery query) {
        log.debug("查询文章分类列表");

        // 构建分页结果对象
        PageResult<ArticleCategoryVO> pageResult = new PageResult<>();
        pageResult.setPageNo(1);       // 当前页码
        pageResult.setPageSize(10);    // 每页数量
        pageResult.setTotal(100L);     //  总记录数

        // 查询数据库获取 PO 列表
        List<ArticleCategoryPO> poList = categoryRepository.pageList(query);
        log.debug("查询文章分类列表结果：{}", poList);

        // 转换 PO -> VO
        List<ArticleCategoryVO> voList = poList.stream()
                .map(po -> {
                    ArticleCategoryVO vo = new ArticleCategoryVO();
                    BeanUtils.copyProperties(po, vo);
                    log.debug("转换文章分类PO -> VO：{}",vo);
                    return vo;
                }).collect(Collectors.toList());

        // 设置到分页结果对象中
        pageResult.setObjects(voList);

        return pageResult;
    }




    @Override
    public void insert(ArticleCategoryParam categoryParam) {

        log.debug("处理新增文章分类请求：业务参数{}", categoryParam);
        ArticleCategoryPO articleCategoryPO = new ArticleCategoryPO();
        BeanUtils.copyProperties(categoryParam,articleCategoryPO);
        log.debug("转换后的PO{}", articleCategoryPO);
        if (articleCategoryPO.getId() == null){//文章类别Id为null,新增文章类别
            articleCategoryPO.setCreateTime(new Date());
            categoryRepository.insert(articleCategoryPO);
        }else{
            articleCategoryPO.setUpdateTime(new Date());

            categoryRepository.update(articleCategoryPO);
        }
    }

    @Override
    public void delete(ArticleCategoryParam categoryParam) {
        log.debug("删除文章分类,参数{}",  categoryParam);
        categoryRepository.delete(categoryParam);
    }

}











